package com.tulele.vtool.service.impl;

import com.tulele.vtool.mapper.ArticleTableInfoMapper;
import com.tulele.vtool.model.BArticleTableInfo;
import com.tulele.vtool.model.combinedEntity.CombinedAritcleTableDetail;
import com.tulele.vtool.model.combinedEntity.CombinedAritcleTableInfo;
import com.tulele.vtool.model.combinedEntity.CombinedCustomerInfo;
import com.tulele.vtool.model.combinedEntity.CombinedCustomerYearInfo;
import com.tulele.vtool.service.ArticleTableInfoService;
import com.tulele.vtool.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class ArticleTableInfoServiceImpl implements ArticleTableInfoService {
    @Autowired
    private ArticleTableInfoMapper articleTableInfoMapper;

    @Override
    public int saveArticleTableInfo(BArticleTableInfo entity) {
        return articleTableInfoMapper.saveArticleTableInfo(entity);
    }

    @Override
    public CombinedAritcleTableInfo getArticleTableInfoById(Integer id) {
        return articleTableInfoMapper.getArticleTableInfoById(id);
    }

    @Override
    public int getArticleTableCountByOwner(Integer userId) {
        Integer total = articleTableInfoMapper.getArticleTableCountByOwner(userId);
        return total == null ? 0 : total.intValue();
    }

    @Override
    public int getLatestThreeDaysCount(Integer userId) {
        Integer total = articleTableInfoMapper.getLatestThreeDaysCount(userId);
        return total == null ? 0 : total.intValue();
    }

    @Override
    public List<CombinedAritcleTableDetail> getArticleTableDetail(Integer userId) {
        return articleTableInfoMapper.getArticleTableDetail(userId);
    }

    @Override
    public List<CombinedCustomerInfo> getCurrentWeekTableData(Integer userId) {
        Date currentDate = DateUtil.getCurDate();
        String firstDayOfWeek = DateUtil.getFirstDayOfWeek(currentDate);
        String lastDayOfWeek = DateUtil.getLastDayOfWeek(currentDate);
        return articleTableInfoMapper.getCurrentWeekTableData(userId, firstDayOfWeek, lastDayOfWeek);
    }

    @Override
    public List<CombinedCustomerInfo> getCurrentMonthTableData(Integer userId) {
        Date currentDate = DateUtil.getCurDate();
        String firstDayOfMonth = DateUtil.getFirstDayOfMonth(currentDate);
        String lastDayOfMonth = DateUtil.getLastDayOfMoth(currentDate);
        return articleTableInfoMapper.getCurrentMonthTableData(userId, firstDayOfMonth, lastDayOfMonth);
    }

    @Override
    public List<CombinedCustomerYearInfo> getCurrentSeasonTableData(Integer userId) {
        // 按天统计
//        String firstDayOfSeason = DateUtil.getFirstDayOfSeason();
//        String lastDayOfSeason = DateUtil.getLastDayOfSeason();
//        return articleTableInfoMapper.getCurrentSeasonTableData(userId, firstDayOfSeason, lastDayOfSeason);

        // 按月统计
        Integer firstMonthOfSeason = Integer.valueOf(DateUtil.getFirstMonthOfSeason());
        Integer lastMonthOfSeason = Integer.valueOf(DateUtil.getLastMonthOfSeason());
        return articleTableInfoMapper.getCurrentSeasonTableDataByMonth(userId, firstMonthOfSeason, lastMonthOfSeason);
    }

    @Override
    public List<CombinedCustomerYearInfo> getCurrentYearTableData(Integer userId) {
        return articleTableInfoMapper.getCurrentYearTableData(userId);
    }

    @Override
    public int getUserCommitStatus(Integer userId, Integer articleId) {
        BArticleTableInfo articleTableInfo = articleTableInfoMapper.getArticleTableByUserId(userId, articleId);

        if (Objects.nonNull(articleTableInfo)) {
            return 1;
        }

        return 0;
    }
}
